#include "widget_setup_local.h"



added_player::added_player( player* pl, QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{
	QHBoxLayout* layout = new QHBoxLayout( this );
	layout->setSpacing( 50 );

	QLabel* player_l = new QLabel( this );
	player_l->setTextFormat( Qt::RichText );
	ostringstream sout;
	sout << "<b><font size=5>Player " << pl->get_id() << ":</font></b>";
	player_l->setText( sout.str().c_str() );
	layout->addWidget( player_l );
	sout.str( "" );

	QLabel* name_l=new QLabel( this );
	name_l->setTextFormat( Qt::RichText );
	sout << "<font size=5>" << pl->get_name() << "</font>";
	name_l->setText( sout.str().c_str() );
	layout->addWidget( name_l );
	sout.str( "" );

	QLabel* type_l = new QLabel( this );
	type_l->setTextFormat( Qt::RichText );
	switch ( pl->get_type() )
	{
	case ai:
		sout << "<font size=5>" << pl->ai_algorithm->get_name() << "</font>";
		type_l->setText( sout.str().c_str() );
		sout.str( "" );
		break;
	case local:
		type_l->setText( "<font size=5>Human player</font>" );
		break;
	case network:
		type_l->setText( "<font size=5>Network player</font>" );
		break;
	}
	layout->addWidget( type_l );

	QLabel* color_l = new QLabel( this );
	color_l->setTextFormat( Qt::RichText );
	sout << "<font size=5>" << game_gui::get_instance()->graphics->get_color( pl->get_id() ) << " color</font>";
	color_l->setText( sout.str().c_str() );
	layout->addWidget( color_l );

	this->setLayout( layout );
};



local_setup_widget::local_setup_widget( QWidget* parent, Qt::WindowFlags flags ): setup_widget( parent, flags )
{	
	layout = new QVBoxLayout( this );


	QWidget* title_w = new QWidget( this );
	QHBoxLayout* title_w_l = new QHBoxLayout( title_w );

	QLabel* title_l = new QLabel( title_w );
	title_l->setTextFormat( Qt::RichText );
	title_l->setText( "<b><font size=8>Local Game Setup</font></b>" );
	title_w_l->addWidget( title_l );

	next_b = new QPushButton( "START", title_w );
	next_b->setFixedWidth( 150 );
	next_b->setEnabled( false );
	title_w_l->addWidget( next_b );
	connect( next_b, SIGNAL( clicked() ), this, SLOT( next() ) );

	title_w->setLayout( title_w_l );
	layout->addWidget( title_w );


	layout->addSpacing( 20 );


	QLabel* add_l = new QLabel( this );
	add_l->setTextFormat( Qt::RichText );
	add_l->setText( "<b><font size=6>Adding of new players</font></b>" );
	layout->addWidget( add_l );


	QWidget* add_w = new QWidget( this );
	QHBoxLayout* add_w_l = new QHBoxLayout( add_w );
	add_w_l->setSpacing( 20 );

	QLabel* name_l = new QLabel( add_w );
	name_l->setTextFormat( Qt::RichText );
	name_l->setText( "<font size=5>Name:</font></b>" );
	add_w_l->addWidget( name_l );

	name_i = new QLineEdit( add_w );
	name_i->setFixedWidth( 250 );
	add_w_l->addWidget( name_i );	

	QLabel* type_l = new QLabel( add_w );
	type_l->setTextFormat( Qt::RichText );
	type_l->setText( "<font size=5>Type:</font></b>" );
	add_w_l->addWidget( type_l );

	type_i = new QComboBox( add_w );
	type_i->addItem( "Human" );
	type_i->addItem( "AI" );
	add_w_l->addWidget( type_i );
	
	add_b = new QPushButton( "Add player", add_w );
	add_w_l->addWidget( add_b );
	connect( add_b, SIGNAL( clicked() ), this, SLOT( add_player() ) );

	add_w->setLayout( add_w_l );
	layout->addWidget( add_w );


	QWidget* algorithm_w = new QWidget( this );
	QHBoxLayout* algorithm_w_l = new QHBoxLayout( algorithm_w );
	algorithm_w_l->setSpacing( 20 );

	QLabel* algorithm_l = new QLabel( algorithm_w );
	algorithm_l->setTextFormat( Qt::RichText );
	algorithm_l->setText( "<font size=5>AI Algorithm:</font></b>" );
	algorithm_w_l->addWidget( algorithm_l );

	algorithm_i = new QComboBox( algorithm_w );
	for( map< string, algorithm* >::iterator it = ai_algorithms::get_instance()->algorithm_list.begin(); it != ai_algorithms::get_instance()->algorithm_list.end(); ++it )
	{
		//skip replay (not real algorithm)
		if ( it->first == "Replay" )
			continue;
		algorithm_i->addItem( ( it->first ).c_str() );
	}
	algorithm_i->setEnabled( false );
	algorithm_w_l->addWidget( algorithm_i );	
	
	algorithm_b = new QPushButton( "Set algorithm", algorithm_w );
	algorithm_b->setEnabled( false );
	algorithm_w_l->addWidget( algorithm_b );
	connect( algorithm_b, SIGNAL( clicked() ), this, SLOT( set_algorithm() ) );

	algorithm_w->setLayout( algorithm_w_l );
	layout->addWidget( algorithm_w );


	QLabel* load_l = new QLabel( this );
	load_l->setTextFormat( Qt::RichText );
	load_l->setText( "<b><font size=6>Map loading</font></b>" );
	layout->addWidget( load_l );


	QWidget* load_w = new QWidget( this );
	QHBoxLayout* load_w_l = new QHBoxLayout( load_w );

	load_i = new QLineEdit( load_w );
	load_i->setFixedWidth( 250 );
	load_i->setText( "maps/map.map" );
	load_w_l->addWidget( load_i );

	load_filename_b = new QPushButton( "...", load_w );
	load_filename_b->setFixedWidth( 20 );
	load_w_l->addWidget( load_filename_b );
	connect( load_filename_b, SIGNAL( clicked() ), this, SLOT( load_filename_map() ) );
	
	load_b = new QPushButton( "Load", load_w );
	load_b->setFixedWidth( 150 );
	load_w_l->addWidget( load_b );
	connect( load_b, SIGNAL( clicked() ), this, SLOT( load_map() ) );

	load_w->setLayout( load_w_l );
	layout->addWidget( load_w );


	layout->addSpacing( 10 );


	QLabel* object_l = new QLabel( this );
	object_l->setTextFormat( Qt::RichText );
	object_l->setText( "<b><font size=6>Objects loading</font></b>" );
	layout->addWidget( object_l );


	QWidget* object_w = new QWidget( this );
	QHBoxLayout* object_w_l = new QHBoxLayout( object_w );

	object_i = new QLineEdit( object_w );
	object_i->setFixedWidth( 250 );
	object_i->setText( "objects/objects.obj" );
	object_w_l->addWidget( object_i );

	object_filename_b = new QPushButton( "...", object_w );
	object_filename_b->setFixedWidth( 20 );
	object_w_l->addWidget( object_filename_b );
	connect( object_filename_b, SIGNAL( clicked() ), this, SLOT( load_filename_objects() ) );

	object_b = new QPushButton( "Load", object_w );
	object_b->setFixedWidth( 150 );
	object_b->setEnabled( false );
	object_w_l->addWidget( object_b );
	connect( object_b, SIGNAL( clicked() ), this, SLOT( load_objects() ) );

	object_w->setLayout( object_w_l );
	layout->addWidget( object_w );


	layout->addStretch();


	this->setLayout( layout );
	//this->setMaximumSize( 608, ( 384 + 45 * parameters::get_instance()->game_parameters[ "MAX_PL" ] ) );
};



void local_setup_widget::add_player()
{
	string nm = name_i->text().remove( ':' ).remove( '\\' ).remove( '=' ).toStdString();	
	players* pla = players::get_instance();
	if ( nm == "" )
	{
		ostringstream sout;
		sout << "Player " << pla->get_players_count();
		nm = sout.str();
		name_i->setText( nm.c_str() );
	}
	if ( type_i->currentText() == "AI" )
	{
		algorithm_b->setEnabled( true );
		algorithm_i->setEnabled( true );
		name_i->setReadOnly( true );
		add_b->setEnabled( false );
	}
	else
	{
		player* pl = new player( pla->get_players_count(), nm, local );
		pla->set_player( pl, pla->get_players_count() );

		added_player* ap = new added_player( pl, this );
		layout->insertWidget( ( layout->count() - 1 ), ap );
		name_i->setText( "" );

		if ( pla->get_players_count() >= parameters::get_instance()->game_parameters[ "MAX_PL" ] )
		{
			add_b->setEnabled( false );
			name_i->setReadOnly( true );
			type_i->setEnabled( false );
		}
		if ( ( pla->get_players_count() >= 2 ) && ( load_b->text() == "OK!" ) )
			next_b->setEnabled( true );
	}	
};



void local_setup_widget::set_algorithm()
{
	add_b->setEnabled( true );
	name_i->setReadOnly( false );
	string nm = name_i->text().remove( ':' ).remove( '\\' ).remove( '=' ).toStdString();	
	players* pla = players::get_instance();
	if ( nm == "" )
	{
		ostringstream sout;
		sout << "Player " << pla->get_players_count();
		nm = sout.str();
		name_i->setText( nm.c_str() );
	}
	if ( type_i->currentText() == "AI" )
	{
		player* pl = new player( pla->get_players_count(), nm, ai );
		pla->set_player( pl, pla->get_players_count() );
		pl->ai_algorithm = ai_algorithms::get_instance()->algorithm_list[ algorithm_i->currentText().toStdString() ]->clone();
		pl->ai_algorithm->init( pl );

		added_player* ap = new added_player( pl, this );
		layout->insertWidget( ( layout->count() - 1 ), ap );
		name_i->setText( "" );

		if ( pla->get_players_count() >= parameters::get_instance()->game_parameters[ "MAX_PL" ] )
		{
			add_b->setEnabled( false );
			name_i->setReadOnly( true );
			type_i->setEnabled( false );
		}

		if ( ( pla->get_players_count() >= 2 ) && ( load_b->text() == "OK!" ) )
			next_b->setEnabled( true );

		algorithm_b->setEnabled( false );
		algorithm_i->setEnabled( false );
	}
};



void local_setup_widget::load_filename_map()
{
	load_i->setText( QFileDialog::getOpenFileName( this, "Load map", "./maps", "Map files (*.map)" ) );
};



void local_setup_widget::load_filename_objects()
{
	object_i->setText( QFileDialog::getOpenFileName( this, "Load objects", "./objects", "Objects files (*.obj)" ) );
};



void local_setup_widget::load_map()
{
	if( mapa::get_instance()->load( load_i->text().toStdString().c_str() ) )
	{
		load_b->setEnabled( false );
		load_filename_b->setEnabled( false );
		load_i->setReadOnly( true );
		object_b->setEnabled( true );
		load_b->setText( "OK!" );
		if ( players::get_instance()->get_players_count() >= 2 )
			next_b->setEnabled( true );
	}
};



void local_setup_widget::load_objects()
{
	if( mapa::get_instance()->load_objects( object_i->text().toStdString().c_str() ) )
	{
		object_b->setEnabled( false );
		object_filename_b->setEnabled( false );
		object_i->setReadOnly( true );
		object_b->setText( "OK!" );
	}
};



void local_setup_widget::next()
{
	game_gui::get_instance()->start_buy();
};